package controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.http.impl.cookie.DateUtils;
import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.SystemOperator;
import models.SystemVUsermenu;
import models.mapper.SystemVUsermenuMapper;
import play.cache.Cache;
import play.mvc.Controller;
import services.CaseService;
import services.MenuServcie;
import utils.BizConstants;
import vos.ResultVo;
import vos.ReturnCode;

/**
 * 首页控制类
 * @author wangshaoming
 *
 */
public class IndexController extends BaseController {
	/**
	 * 跳转到首页
	 */
	public static void index(){
		SystemOperator oper=getLoginUser();
		renderArgs.put("userRole", oper.getRoleId());
		render();
	}
	 /**
     * 获取风琴菜单
     *
     * @Title: getAccordion 
     * @return: void
     */
    public static void getAccordion(){
    	SystemOperator oper=getLoginUser();
    	//从缓存中获取,存在就返回
    	List<SystemVUsermenu> usermenu=(List<SystemVUsermenu>)Cache.get(oper.getOperatorid()+"_menu");
    	if(usermenu!=null&&usermenu.size()>0){
    		renderJSON(usermenu);
    	}
    	usermenu=new ArrayList<SystemVUsermenu>();
    	//获取菜单树
    	List<SystemVUsermenu> menus=MenuServcie.getMenus("",oper.getOperatorid());
    	for(SystemVUsermenu menu : menus){
    		List<SystemVUsermenu> children=MenuServcie.getMenus(menu.getMenuid(),oper.getOperatorid());
    		menu.children=children;
    		usermenu.add(menu);
    	}
    	Cache.set(oper.getOperatorid()+"_menu",usermenu);
    	renderJSON(usermenu);
    }
    /**
     * 获取今日开庭和闭庭数量
     */
    public static void getTodayCourt(){
    	Date date=new Date();
    	String today=DateUtils.formatDate(date, "yyyy-MM-dd");
    	long courtLiveCount=CaseService.countTodayCourt(BizConstants.CASE_STATUS_START,today);
    	long courtStopCount=CaseService.countTodayCourt(BizConstants.CASE_STATUS_STOP,today);
    	Map<String,Object> map=new HashMap<String,Object>();
    	map.put("courtLiveCount", courtLiveCount);
    	map.put("courtStopCount", courtStopCount);
    	ResultVo vo=new ResultVo(ReturnCode.SuccessCode,"获取成功",map);
    	renderJSON(vo);
    }
    /**
     * 跳转到控制首页
     */
    public static void contrIndex(){
    	SystemOperator oper=getLoginUser();
    	//判断如果是管理员进admin的首页
    	if(BizConstants.ADMIN_ROLE.equals(oper.getRoleId())){
    		render("IndexController/adminIndex.html");
    	}
    	//如果是书记员进入书记员首页
    	else if(BizConstants.CLERT_ROLE.equals(oper.getRoleId())){
    		render("IndexController/contrIndex.html");
    	}
//    	else if(BizConstants.SERVICE_ROLE.equals(oper.getRoleId())){
//    		render("IndexController/serviceIndex.html");
//    	}
    }
    /**
     * 更新用户密码
     */
    public static void updatePassword(){
    	render();
    }
}
